//
//  MyCollectController.m
//  LeaveLight
//
//  Created by 张 on 2019/3/6.
//  Copyright © 2019年 xsl. All rights reserved.
//

#import "MyCollectController.h"
#import "FundListCell.h"

@interface MyCollectController ()

@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NSMutableArray * dataArr;

@property (nonatomic, assign) BOOL isEdit;//是否在编辑状态
@property (nonatomic, strong) NSMutableArray * chooseArr;

@end

@implementation MyCollectController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = UIColorMakeWithHex(@"#F5F5F5");
    [self setTitle:@"我的收藏"];
    [self setRightButtonWithTitle:@"编辑" image:[UIImage imageNamed:@""] himage:[UIImage imageNamed:@""] action:@selector(deleteCollect:)];
    
    self.viewTop.constant = NavigationContentTopConstant + 1;
    [self.tableView registerNib:[UINib nibWithNibName:@"FundListCell" bundle:nil] forCellReuseIdentifier:NSStringFromClass([FundListCell class])];
    self.tableView.estimatedRowHeight = 330;
    
    self.page = 1;
    [self creatHeaderFooter];
    
    self.isEdit = NO;
    self.chooseArr = [NSMutableArray array];
    
    [self.tableView reloadData];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView.mj_header beginRefreshing];
}

- (void)requestDataWithloadMore:(BOOL)loadMore
{
    weakify(self);
    if (loadMore) {
        self.page ++;
    }else{
        self.page = 1;
    }
    
    [self getCollectListWithPagesize:20 index:self.page Success:^(NSMutableArray * _Nonnull listArr) {
        strongify(self);
        if (self.page == 1) {
            if (listArr.count == 0) {
                TempView * temp = [[TempView alloc]initWithFrame:CGRectMake(0, 0, self.tableView.qmui_width, SCREEN_HEIGHT) img:@"收藏空白页" placeStr:@"你的瓶子空空如也"];
                [self.tableView addSubview:temp];
                [self.tableView.mj_header endRefreshing];
            }else{
                self.dataArr = [NSMutableArray arrayWithArray:listArr];
                [self.tableView.mj_header endRefreshing];
                [self.tableView reloadData];
            }
        }else{
            [self.dataArr addObjectsFromArray:listArr];
            [self.tableView reloadData];
            if (listArr.count < 20) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [self.tableView.mj_footer endRefreshing];
            }
        }
    } failed:^(NSString * _Nonnull error) {
        strongify(self);
        [QMUITips showError:error];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
    
}

- (void)creatHeaderFooter
{
    weakify(self);
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        strongify(self);
        [self.tableView.mj_footer resetNoMoreData];
        [self requestDataWithloadMore:NO];
    }];
    MJRefreshAutoNormalFooter * footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        strongify(self);
        [self requestDataWithloadMore:YES];
    }];
    [footer setTitle:@"" forState:MJRefreshStateIdle];
    [footer setTitle:@"" forState:MJRefreshStatePulling];
    self.tableView.mj_footer = footer;
}

- (void)deleteCollect:(UIButton *)btn
{
    if (self.isEdit) {
        [btn setTitle:@"编辑" forState:0];
        self.isEdit = NO;
        if (self.chooseArr.count > 0) {
            NSMutableArray * arr = [NSMutableArray array];
            for (TemplateModel * model in self.chooseArr) {
                [arr addObject:model.chicken_id];
            }
            weakify(self);
            [self deleteCollectWithid:[arr componentsJoinedByString:@","] Success:^{
                strongify(self);
                [self requestDataWithloadMore:NO];
            } failed:^(NSString * _Nonnull error) {
                [QMUITips showWithText:error];
            }];
        }
    }else{
        [btn setTitle:@"删除" forState:0];
        self.isEdit = YES;
    }
    [self.chooseArr removeAllObjects];
    [self.tableView reloadData];
}

#pragma mark - tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CollectModel * model = self.dataArr[indexPath.row];
    FundListCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([FundListCell class])];
    TemplateModel *templateModel = [[TemplateModel alloc] init];
    templateModel.chicken_model = model.chicken_data;
    [cell updateUIWithModel:templateModel];
    if(self.isEdit){
        [cell.chooseImg setHidden:NO];
        if ([self.chooseArr containsObject:model]) {
            [cell.chooseImg setImage:[UIImage imageNamed:@"选择_sel"]];
        }else{
            [cell.chooseImg setImage:[UIImage imageNamed:@"选择"]];
        }
    }else{
        [cell.chooseImg setHidden:YES];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    TemplateModel *templateModel = [[TemplateModel alloc] init];
    CollectModel * model = self.dataArr[indexPath.row];
    if (self.isEdit) {
        if ([self.chooseArr containsObject:model]) {
            [self.chooseArr removeObject:model];
        }else{
            [self.chooseArr addObject:model];
        }
        [tableView reloadData];
    }else{
        templateModel.chicken_model = model.chicken_data;
        EditController * edit = [[EditController alloc]initWithNibName:@"EditController" bundle:nil];
        edit.model = templateModel;
        [self.navigationController pushViewController:edit animated:YES];
    }
    
}
    
    

@end
